package com.springmvc.controller;

import com.springmvc.entity.File;
import com.springmvc.service.FileService;
import com.springmvc.way.Msfile;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

/**
 * Created by 29047 on 2018/3/11.
 */
@Controller
public class fileController {

    @Autowired
    private FileService fileService;
    @Autowired
    private HttpServletRequest request;

    //新增文件
    @RequestMapping(value = "/file/add",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject fileAdd(@RequestParam("newfile") String newfile,@RequestParam("file") MultipartFile file){
        JSONObject json = new JSONObject();
        JSONObject filedetail = JSONObject.fromObject(newfile);
        File filemation = new File();

        Iterator it = filedetail.keys();
        while (it.hasNext()){
            String key = it.next().toString();
            if (key.equals("name")){
                filemation.setFileName(filedetail.getString(key));
            }
            if (key.equals("createby")){
                filemation.setCreateBy(Integer.parseInt(filedetail.getString(key)));
            }
        }

        Date date = new Date();
        filemation.setCreateTime(date);

        Msfile msfile = new Msfile(file,request);
        filemation.setFileAddress(msfile.saveFile());
        filemation.setFileAccount(0);

        if (fileService.insert(filemation) == 1){
            json.put("result",1);   //成功返回result=1
        } else {
            json.put("result",0);   //失败返回result=0
        }
        return json;
    }

    //删除文件
    @RequestMapping(value = "/file/delete",method = RequestMethod.POST)
    @ResponseBody
    private JSONObject fileDelete(@RequestParam("fileId") String fileId){
        JSONObject json = new JSONObject();

        if (fileService.deleteByPrimaryKey(Integer.parseInt(fileId)) == 1){
            json.put("result",1);   //成功返回result=1
        } else {
            json.put("result",0);   //失败返回result=0
        }
        return json;
    }

    //文件列表
    @RequestMapping(value = "/file/list",method = RequestMethod.GET)
    @ResponseBody
    private JSONObject fileList(){
        JSONObject json = new JSONObject();
        ArrayList<File> filelist = fileService.selectlist();
        json.put("data",filelist);
        return json;
    }

    //点击量
    @RequestMapping(value = "/file/count",method = RequestMethod.POST)
    @ResponseBody
    private void fileCount(@RequestParam("fileId") String fileId){

        File file = fileService.selectByPrimaryKey(Integer.parseInt(fileId));
        int newcount = file.getFileAccount()+1;
        file.setFileAccount(newcount);
        fileService.updateByPrimaryKeySelective(file);
    }
}
